﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Telerik.Web.UI;

public partial class Posts_Menus : BasePostPage
{
    public override Role.Roles[] AllowedRoles
    {
        get
        {
            return new Role.Roles[] { Role.Roles.ContentViewer, Role.Roles.ContentEditor, Role.Roles.Developer, Role.Roles.ContentPublisher };
        }
    }

    protected Role.Roles[] EditRoles
    {
        get
        {
            return new Role.Roles[] { Role.Roles.ContentEditor, Role.Roles.Developer, Role.Roles.ContentPublisher };
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lnkEdit.Visible = User.HasRole(EditRoles);
            lnkSave.Visible = false;
            lnkCancel.Visible = false;
        }
    }

    protected void dsMenus_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Menu> e)
    {
        e.SetData(Acms.Bll.Menu.GetAllBySiteId(AcmsContext.SiteId));
    }
    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        foreach (GridItem item in gridMenus.Items)
        {
            gridMenus.EditIndexes.Add(item.ItemIndex);
        }
        gridMenus.DataBind();

        lnkEdit.Visible = false;
        lnkSave.Visible = true;
        lnkCancel.Visible = true;
    }

    protected void lnkSave_Click(object sender, EventArgs e)
    {
        Guid transactionKey = Guid.Empty;
        foreach (GridItem item in gridMenus.Items)
        {
            GridDataItem editItem = item as GridDataItem;
            if (editItem != null)
            {
                int menuId = (int)editItem.GetDataKeyValue("Id");
                PostMenu link = GetLink(menuId);
                CheckBox cb = editItem.FindControl("cbIncluded") as CheckBox;
                if (cb != null)
                {
                    bool save = false;
                    if (link == null && cb.Checked)
                    {
                        link = PostMenu.CreateInstance(PostId, menuId, cb.Checked);
                        CurrentPostMenu.Add(link);
                        save = true;
                    }
                    else if (link != null && link.Included && !cb.Checked)
                    {
                        link.Included = false;
                        save = true;
                    }
                    else if (link != null && !link.Included && cb.Checked)
                    {
                        link.Included = true;
                        save = true;
                    }

                    if (save)
                    {
                        if (transactionKey == Guid.Empty)
                        {
                            link.BeginTransaction();
                            transactionKey = link.TransactionKey;
                        }
                        else
                        {
                            link.UseTransaction(transactionKey);
                        }
                        link.Update();
                    }

                }
            }
        }
        if (transactionKey != Guid.Empty)
        {
            PostMenu tmp = PostMenu.CreateInstance();
            tmp.UseTransaction(transactionKey);
            tmp.CommitTransaction();
        }

        gridMenus.EditIndexes.Clear();
        gridMenus.DataBind();

        lnkEdit.Visible = true;
        lnkSave.Visible = false;
        lnkCancel.Visible = false;
    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        gridMenus.EditIndexes.Clear();
        gridMenus.DataBind();

        lnkEdit.Visible = true;
        lnkSave.Visible = false;
        lnkCancel.Visible = false;
    }

    private List<PostMenu> currentPostMenu;
    protected List<PostMenu> CurrentPostMenu
    {
        get 
        {
            if(currentPostMenu == null)
            {
                currentPostMenu = PostMenu.GetAllByPostId(PostId);
            }
            return currentPostMenu; 
        }
    }

    protected PostMenu GetLink(int menuId)
    {
        foreach (PostMenu link in CurrentPostMenu)
        {
            if (link.MenuId == menuId)
            {
                return link;
            }
        }
        return null;
    }

    protected void gridMenus_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsDataBound)
        {
            CheckBox cb = e.Item.FindControl("cbIncluded") as CheckBox;
            cb.Enabled = e.Item.IsInEditMode;
            Acms.Bll.Menu menu = e.Item.DataItem as Acms.Bll.Menu;
            PostMenu link = GetLink(menu.Id);
            if (link != null && link.Included)
            {
                if (cb != null)
                {
                    cb.Checked = true;
                }
            }
        }
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Menus");
    }

}
